package com.itheima.Service.Impl;

import com.itheima.Service.OrderSettingService;
import com.itheima.mapper.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingDto;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.*;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void downloadFile(HttpServletResponse response) throws  IOException{
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("templates/ordersetting_template.xlsx");

        response.setContentType("application/vnd.ms-excel"); // 或根据实际文件类型设置
        response.setHeader("Content-Disposition", "attachment;filename=orderSetting.xls");

        XSSFWorkbook excel = new XSSFWorkbook();

        try (OutputStream outputStream = response.getOutputStream()) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.flush();
        } finally {
            inputStream.close();
        }

    }


            @Override
    public List<OrderSettingDto> getOrderSettingByMonth(String month) {
             String[]time= month.split("-");
             YearMonth yearMonth=YearMonth.of(Integer.parseInt(time[0]),Integer.parseInt(time[1]));
             LocalDate  begin=yearMonth.atDay(1);
             LocalDate end=yearMonth.atEndOfMonth();
        List<OrderSetting> result= orderSettingDao.getOrderSettingByMonth(begin,end);
        List<OrderSettingDto> orderSettingDtoList=new ArrayList<>();
        for (OrderSetting orderSetting : result) {
            OrderSettingDto orderSettingDto=new OrderSettingDto();
             orderSettingDto.setDate(orderSetting.getOrderDate().getDate());
             orderSettingDto.setNumber(orderSetting.getNumber());
             orderSettingDto.setReservations(orderSetting.getReservations());
            orderSettingDtoList.add(orderSettingDto);
        }
        return orderSettingDtoList;
    }

    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();


        orderSettingDao.editNumberByOrderDate(orderSetting);




    }

    @Override
    public void uploadFile(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();

        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);


        Sheet sheet = workbook.getSheetAt(0);

        Iterator<Row> iterator = sheet.iterator();

        while (iterator.hasNext()) {
            Row row = iterator.next();
            if (row.getRowNum() == 0) {
                continue;
            }

           Date date=row.getCell(0).getDateCellValue();
           Integer numberCell=  (int) row.getCell(1).getNumericCellValue();
            orderSettingDao.updateBaseOnExcel(date, numberCell);
        }


    }
}
